﻿var labelType, useGradients, nativeTextSupport, animate;
(function() {
	var ua = navigator.userAgent, iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i), typeOfCanvas = typeof HTMLCanvasElement, nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'), textSupport = nativeCanvasSupport && ( typeof document.createElement('canvas').getContext('2d').fillText == 'function');
	//I'm setting this based on the fact that ExCanvas provides text support for IE
	//and that as of today iPhone/iPad current text support is lame
	labelType = (!nativeCanvasSupport || (textSupport && !iStuff)) ? 'Native' : 'HTML';
	nativeTextSupport = labelType == 'Native';
	useGradients = nativeCanvasSupport;
	animate = !(iStuff || !nativeCanvasSupport);
})();

var Log = {
	elem : false,
	write : function(text) {
		if (!this.elem)
			this.elem = document.getElementById('log');
		this.elem.innerHTML = text;
		this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
	}
};

var a = false;

function init(nodeId) {
	//init data
	var json = {
		id : "190_0",
		name : "U信业务平台",
		children : [{
			id : "236797_5",
			name : "VM-3 Power710-C",
			data : {
				"$color" : '#ff0000',
				relation : "<h4>Jeff Ament</h4>"
			}
		}, {
			id : "41529_12",
			name : "VM-4 Power710-C",
			data : {
				"$color" : 'blue',
				relation : "<h4>Stone Gossard</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Mother Love Bone <div>(relation: member of band)</div></li><li>Brad <div>(relation: member of band)</div></li><li>Green River <div>(relation: member of band)</div></li><li>Gossman Project <div>(relation: member of band)</div></li></ul>"
			},
			children : []
		}, {
			id : "236583_23",
			name : "产品中心",
			data : {
				relation : "<h4>Mike McCready</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Mad Season <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>$10,000 Gold Chain <div>(relation: collaboration)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>The Rockfords <div>(relation: member of band)</div></li><li>Gossman Project <div>(relation: member of band)</div></li></ul>"
			},
			children : [{
				id : "1744_24",
				name : "手机报业务平台",
				data : {
					relation : "<h4>Mad Season</h4><b>Connections:</b><ul><li>Mike McCready <div>(relation: member of band)</div></li></ul>"
				},
				children : []
			}, {
				id : "1756_25",
				name : "移动营销业务平台",
				data : {
					relation : "<h4>Temple of the Dog</h4><b>Connections:</b><ul><li>Mike McCready <div>(relation: member of band)</div></li></ul>"
				},
				children : [{
					id : "236585_430",
					name : "VM-C Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}, {
					id : "236585_540",
					name : "VM-B Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}, {
					id : "236585_650",
					name : "VM-A Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}]
			}, {
				id : "43661_26",
				name : "智能分析平台",
				data : {
					relation : "<h4>$10,000 Gold Chain</h4><b>Connections:</b><ul><li>Mike McCready <div>(relation: collaboration)</div></li></ul>"
				},
				children : [{
					id : "236585_330",
					name : "VM-A1 Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}, {
					id : "236585_340",
					name : "VM-A2 Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}, {
					id : "236585_350",
					name : "VM-A3 Power710-C",
					data : {
						relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
					},
					children : []
				}]
			}]
		}, {
			id : "236585_30",
			name : "VM-1 Power710-C",
			data : {
				relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
			},
			children : []
		}, {
			id : "236585_38",
			name : "VM-5 Power710-C",
			data : {
				relation : "<h4>VM-1 Power710-C</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Soundgarden <div>(relation: member of band)</div></li><li>Temple of the Dog <div>(relation: member of band)</div></li><li>Eleven <div>(relation: supporting musician)</div></li><li>Queens of the Stone Age <div>(relation: member of band)</div></li><li>Wellwater Conspiracy <div>(relation: member of band)</div></li><li>M.A.C.C. <div>(relation: collaboration)</div></li><li>Tone Dogs <div>(relation: member of band)</div></li></ul>"
			},
			children : []
		}, {
			id : "236612_45",
			name : "VM-2 Power710-C",
			data : {
				relation : "<h4>Jack Irons</h4><b>Connections:</b><ul><li>Pearl Jam <div>(relation: member of band)</div></li><li>Redd Kross <div>(relation: member of band)</div></li><li>Eleven <div>(relation: member of band)</div></li><li>Red Hot Chili Peppers <div>(relation: member of band)</div></li><li>Anthym <div>(relation: member of band)</div></li><li>What Is This? <div>(relation: member of band)</div></li></ul>"
			},
			children : []
		}],

	};
	//end

	//init RGraph
	var rgraph = new $jit.RGraph({
		//Where to append the visualization
		injectInto : 'infovis',
		//Optional: create a background canvas that plots
		//concentric circles.
		background : {
			CanvasStyles : {
				strokeStyle : '#555'
			}
		},
		//Add navigation capabilities:
		//zooming by scrolling and panning.
		Navigation : {
			enable : true,
			panning : true,
			zooming : 10
		},
		//Set Node and Edge styles.
		Node : {
			overridable : true,
			dim : 5
			//color: '#ddeeff'
		},

		Edge : {
			color : '#C17878',
			lineWidth : 1.5
		},

		onBeforeCompute : function(node) {
			//Add the relation list in the right column.
			//This list is taken from the data property of each JSON node.
			//$jit.id('inner-details').innerHTML = node.data.relation;
		},

		//Add the name of the node in the correponding label
		//and a click handler to move the graph.
		//This method is called once, on label creation.
		onCreateLabel : function(domElement, node) {
			var vm_name = "VM";
			//my add
			var strvm_name = node.name.substr(0, 2);
			//my add
			domElement.innerHTML = node.name;
			domElement.onclick = function() {
				if(vm_name == strvm_name){//虚拟机，需
					window.open('index_back.html', '_self');
				}else{//普通部门节点
					rgraph.onClick(node.id);
				}
			};
		},
		//Change some label dom properties.
		//This method is called each time a label is plotted.
		onPlaceLabel : function(domElement, node) {
			domElement.style.fontSize = '12px';
			domElement.style.textAlign = 'left';
			domElement.innerHTML = node.name.split(" ").join('<br>');
		}
	});
	//load JSON data
	rgraph.loadJSON(json);
	//trigger small animation
	rgraph.graph.eachNode(function(n) {
		var pos = n.getPos();
		pos.setc(-200, -200);
	});
	
	rgraph.compute('end');
	
	rgraph.fx.animate({
		modes : ['polar'],
		duration : 2000
	});
	nodeId && rgraph.onClick(nodeId);
	//end
	//append information about the root relations in the right column
	// $jit.id('inner-details').innerHTML = rgraph.graph.getNode(rgraph.root).data.relation;
}
